
// 表单校验
$(function(){
	
	// 首页大图
	$("#banner-form").validate({
		debug: false, 
        focusInvalid: false,   
        onkeyup: false,   
        submitHandler: function(form){    
            form.submit();   //提交表单   
        },   
		rules : {
			image : {
				required:true
			},
			link : {
				required:true,
				url: true
			}
		},
		messages : {
			image : {
				required:"请上传图片"
			},
			link : {
				required:"请输入链接",
				url: "链接格式错误"
			}
		},
		errorClass: "help-inline",
		errorElement: "span",
		highlight:function(element, errorClass, validClass) {
			$(element).parents('.control-group').addClass('error');
		},
		unhighlight: function(element, errorClass, validClass) {
			$(element).parents('.control-group').removeClass('error');
			$(element).parents('.control-group').addClass('success');
		}
	});
	
	// 资讯
	$("#news-form").validate({
		debug: false, 
        focusInvalid: false,   
        onkeyup: false,   
        submitHandler: function(form){    
            form.submit();   //提交表单   
        },
        rules:{
        	title:{
        		required:true,
        		maxlength:100
        	},
        	descr:{
        		required:true,
        		maxlength:200
        	},
        	context:{
        		required:true,
        		minlength:100
        	},
        	tag:{
        		required:function(){return ($("#color").val()!=null&&$("#color").val()!='')},
        	    maxlength:5
        	},
        	color:{
        		required:function(){return ($("#tag").val()!=null&&$("#tag").val()!='')},
        		maxlength:20
        	}
        },
        messages : {
			title : {
				required:"请填写标题",
				maxlength:"长度不超过100个字符"
			},
			descr : {
				required:"请填写描述",
				maxlength:"长度不超过200个字符"
			},
			context : {
				required:"请填写正文",
				minlength:"长度最少为100个字符"
			},
			tag:{
        		required:"请填写标签",
        		maxlength:"长度不超过5个字符"
        	},
        	color:{
        		required:"请填写标签颜色",
        		maxlength:"长度不超过20个字符"
        	}
		},
		errorClass: "help-inline",
		errorElement: "span",
		highlight:function(element, errorClass, validClass) {
			$(element).parents('.control-group').addClass('error');
		},
		unhighlight: function(element, errorClass, validClass) {
			$(element).parents('.control-group').removeClass('error');
			$(element).parents('.control-group').addClass('success');
		}
	});
	
	// 旅游
	$("#tour-form").validate({
		debug: false, 
        focusInvalid: false,   
        onkeyup: false,   
        submitHandler: function(form){    
            form.submit();   //提交表单   
        },
        rules:{
        	zone:{
        		required:true
        	},
        	title:{
        		required:true,
        		maxlength:100
        	},
        	descr:{
        		required:true,
        		maxlength:200
        	},
        	image : {
				required:true
			},
        	context:{
        		required:true,
        		minlength:100
        	},
        	tag:{
        		required:function(){return ($("#color").val()!=null&&$("#color").val()!='')},
        	    maxlength:5
        	},
        	color:{
        		required:function(){return ($("#tag").val()!=null&&$("#tag").val()!='')},
        		maxlength:20
        	}
        },
        messages : {
        	zone:{
        		required:"请选择地区"
        	},
			title : {
				required:"请填写标题",
				maxlength:"长度不超过100个字符"
			},
			descr : {
				required:"请填写描述",
				maxlength:"长度不超过200个字符"
			},
			image : {
				required:"请上传图片"
			},
			context : {
				required:"请填写正文",
				minlength:"长度最少为100个字符"
			},
			tag:{
        		required:"请填写标签",
        		maxlength:"长度不超过5个字符"
        	},
        	color:{
        		required:"请填写标签颜色",
        		maxlength:"长度不超过20个字符"
        	}
		},
		errorClass: "help-inline",
		errorElement: "span",
		highlight:function(element, errorClass, validClass) {
			$(element).parents('.control-group').addClass('error');
		},
		unhighlight: function(element, errorClass, validClass) {
			$(element).parents('.control-group').removeClass('error');
			$(element).parents('.control-group').addClass('success');
		}
	});
	// 旅游
	$("#prod-form").validate({
		debug: false, 
        focusInvalid: false,   
        onkeyup: false,   
        submitHandler: function(form){    
            form.submit();   //提交表单   
        },
        rules:{
        	type:{
        		required:true
        	},
        	name:{
        		required:true,
        		maxlength:100
        	},
        	descr:{
        		required:true,
        		maxlength:200
        	},
        	image : {
				required:true
			},
        	context:{
        		required:true,
        		minlength:100
        	},
        	tag:{
        		required:function(){return ($("#color").val()!=null&&$("#color").val()!='')},
        	    maxlength:5
        	},
        	color:{
        		required:function(){return ($("#tag").val()!=null&&$("#tag").val()!='')},
        		maxlength:20
        	}
        },
        messages : {
        	type:{
        		required:"请选择产品类型"
        	},
			name : {
				required:"请填写产品名称",
				maxlength:"长度不超过100个字符"
			},
			descr : {
				required:"请填写描述",
				maxlength:"长度不超过200个字符"
			},
			image : {
				required:"请上传图片"
			},
			context : {
				required:"请填写正文",
				minlength:"长度最少为100个字符"
			},
			tag:{
        		required:"请填写标签",
        		maxlength:"长度不超过5个字符"
        	},
        	color:{
        		required:"请填写标签颜色",
        		maxlength:"长度不超过20个字符"
        	}
		},
		errorClass: "help-inline",
		errorElement: "span",
		highlight:function(element, errorClass, validClass) {
			$(element).parents('.control-group').addClass('error');
		},
		unhighlight: function(element, errorClass, validClass) {
			$(element).parents('.control-group').removeClass('error');
			$(element).parents('.control-group').addClass('success');
		}
	});
	
	// 资讯
	$("#notice-form").validate({
		debug: false, 
        focusInvalid: false,   
        onkeyup: false,   
        submitHandler: function(form){    
            form.submit();   //提交表单   
        },
        rules:{
        	type:{
        		required:true
        	},
        	title:{
        		required:true,
        		maxlength:100
        	},
        	descr:{
        		required:true,
        		maxlength:200
        	},
        	context:{
        		required:true,
        		minlength:100
        	},
        	tag:{
        		required:function(){return ($("#color").val()!=null&&$("#color").val()!='')},
        	    maxlength:5
        	},
        	color:{
        		required:function(){return ($("#tag").val()!=null&&$("#tag").val()!='')},
        		maxlength:20
        	}
        },
        messages : {
        	type:{
        		required:"请选择类型"
        	},
			title : {
				required:"请填写标题",
				maxlength:"长度不超过100个字符"
			},
			descr : {
				required:"请填写描述",
				maxlength:"长度不超过200个字符"
			},
			context : {
				required:"请填写正文",
				minlength:"长度最少为100个字符"
			},
			tag:{
        		required:"请填写标签",
        		maxlength:"长度不超过5个字符"
        	},
        	color:{
        		required:"请填写标签颜色",
        		maxlength:"长度不超过20个字符"
        	}
		},
		errorClass: "help-inline",
		errorElement: "span",
		highlight:function(element, errorClass, validClass) {
			$(element).parents('.control-group').addClass('error');
		},
		unhighlight: function(element, errorClass, validClass) {
			$(element).parents('.control-group').removeClass('error');
			$(element).parents('.control-group').addClass('success');
		}
	});
	// 密码修改
	$("#password-form").validate({
		debug: false, 
        focusInvalid: false,   
        onkeyup: false,   
        submitHandler: function(form){    
        	$(form).ajaxSubmit({
        		url: ctx+'/password/update',   
        	    type: 'post',             
        	    dataType: 'json',              
        	    data:$("#password-form").serialize(),
        	    success:function(data){
        	    	if(data.result_code=='SUC'){
        	    		layer.alert("修改成功");
        	    		var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
                        parent.layer.close(index);
        	    	}
        	    },
        	    error:function(){
        	    	layer.msg("修改失败");
        	    }
        	});
        },   
		rules : {
			password : {
				required:true,
				rangelength:[6,20],
				isPwd:true
			},
			rePassword : {
				required:true,
				equalTo: "#password"
			}
		},
		messages : {
			password : {
				required:"请输入新密码",
				rangelength:"新密码为6-20个字符",
				isPwd:'以字母开头，长度在6-20之间，只能包含字符、数字和下划线'
			},
			rePassword : {
				required:"请输入确认密码",
				equalTo:"确认密码不正确"
			}
		},
		errorClass: "help-inline",
		errorElement: "span",
		highlight:function(element, errorClass, validClass) {
			$(element).parents('.control-group').addClass('error');
		},
		unhighlight: function(element, errorClass, validClass) {
			$(element).parents('.control-group').removeClass('error');
			$(element).parents('.control-group').addClass('success');
		}
	});
});